﻿#region 程序集引用
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
#endregion

#region 版权注释
/*===========================================================================
// Copyright (C) 2017 Zhang.cn，并保留所有 
// 作者：吴岸标 QQ:405403610
// 创建日期：2017-09-03
// 功能描述： 文件日志
===========================================================================*/
#endregion

namespace Zhang
{
    namespace Utils
    {
        /// <summary>
        /// 文件日志
        /// </summary>
        public class FileLog
        {

            /// <summary>
            /// 日志文件文件夹
            /// </summary>
            private static string _folder;
            
            static FileLog()
            {
                _folder = AppDomain.CurrentDomain.BaseDirectory + "logs\\";

                if (false == Directory.Exists(_folder))
                {
                    Directory.CreateDirectory(_folder);
                }
            }

            /// <summary>
            /// 添加日志
            /// </summary>
            /// <param name="logContent">日志内容</param>
            public static void Append(string logContent, string type = "logs")
            {
                lock (type)
                {
                    string logFilePath, dirPath;

                    if (type == "logs")
                    {
                        logFilePath = _folder + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    }
                    else
                    {
                        dirPath = _folder + type + "\\";
                        if (false == Directory.Exists(dirPath))
                        {
                            Directory.CreateDirectory(dirPath);
                        }

                        logFilePath = dirPath + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    }

                    if (!File.Exists(logFilePath))
                    {
                        FileStream stream = File.Create(logFilePath);
                        stream.Close();
                        stream.Dispose();
                    }
                    using (StreamWriter writer = new StreamWriter(logFilePath, true))
                    {
                        writer.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + "]");
                        writer.WriteLine(logContent);
                        writer.WriteLine("-----------------------------------------------------------------------------------------------------------------------------------------------");
                    }
                }
            }

            public static void AddLgoToText(string filePath,string content)
            {
                if (!System.IO.File.Exists(filePath))
                {
                    FileStream stream = System.IO.File.Create(filePath);
                    stream.Close();
                    stream.Dispose();
                }

                using (StreamWriter writer = new StreamWriter(filePath, true))
                {
                    writer.WriteLine(content);
                }
            }

        }
    }
}
